package club.easin.product.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import club.easin.product.bean.Fault;
import club.easin.product.service.FaultService;
import club.easin.utils.ReturnData;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("fault")
public class FaultController {
	
	@Autowired
	private FaultService faultService;
	
	@RequestMapping(value="getNameAndCheck",method=RequestMethod.POST,produces="application/x-www-form-urlencoded;charset=UTF-8")
	public void getNameAndCheck(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		String param=request.getParameter("params");
		JSONObject object=JSONObject.fromObject(param);
		String phenomenon=object.getString("phenomenon");
		String rangeName=object.getString("ranges");
		ReturnData data=new ReturnData();
		try {
			data.setCode(200);
			data.setDesc("fault");
			List<Map<String,Object>> list=this.faultService.getFalultByPhenomenon(phenomenon,rangeName);
			List<String> typeList=new ArrayList<String>();
			String name="";
			for (Map<String,Object> map : list) {
				typeList.add((String)map.get("type"));
				name=(String)map.get("name");
			}
			Map<String,Object> map=new HashMap<String, Object>();
			map.put("type",typeList);
			map.put("name",name);
			data.setResult(map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			data.setCode(500);
			data.setDesc("fault");
			data.setResult("");
		}
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter writer=response.getWriter();
		JSONObject jsonObject=JSONObject.fromObject(data);
		writer.write(jsonObject.toString());
		
	}
	
	//获取类型产生的原因和解决办法
	@RequestMapping(value="getReasonAndWay",method=RequestMethod.POST,produces="application/x-www-form-urlencoded;charset=UTF-8")
	public void getReasonAndWay(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String param=request.getParameter("params");
		JSONObject object=JSONObject.fromObject(param);
		String type=object.getString("type");
		ReturnData data=new ReturnData();
		try {
			data.setCode(200);
			data.setDesc("fault");
			List<Map<String,Object>> list=this.faultService.getReasonAndWay(type);
			List<String> typeList=new ArrayList<String>();
			Map<String,Object> map=new HashMap<String, Object>();
			map.put("type",typeList);
			data.setResult(map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			data.setCode(500);
			data.setDesc("fault");
			data.setResult("");
		}
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter writer=response.getWriter();
		JSONObject jsonObject=JSONObject.fromObject(data);
		writer.write(jsonObject.toString());
	}

}
